// pages/rescue/shop/shop.js
const app = getApp()
var xrsdk = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    shopList: [],
    shop_id: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      latitude  : options.latitude,
      longitude : options.longitude,
      service_id: options.service_id
    });
    console.log(this.data.latitude);
    console.log(this.data.longitude);
    that.initShop();
  },
  initShop:function(){
    var that = this;
    xrsdk.request(app, {
      url: config.service.get_shop_rescue_list,
      login: false,
      data: {
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        service_id:that.data.service_id,
        page: that.data.pageIndex,
        page_size: 10
      },
      success: function(res) {
        if (res.data.data.has_more == false) {
          that.setData({
            pullUpOn: false
          })
        }
        that.setData({
          shopList: that.data.shopList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        wx.stopPullDownRefresh()
      },
      fail: function(res) {},
    })
  },
  initData: function() {
    this.setData({
      shopList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      shop_id: 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  selectRadio: function(e) {
    console.log(e);
    this.setData({
      shop_id: e.detail.value
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData();
    this.initShop();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initShop();
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  btnPay: function(e) {
    var that = this;
    if (that.data.shop_id == 0 || that.data.shop_id < 1) {
      util.toast('请选择门店');
      return;
    }
    var row = that.data.shopList.find(that.getShopItem);
    if (row == null || row == undefined) {
      util.toast('请选择门店');
      return;
    }
    //console.log(row);
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      shop: row
    }, function() {
      wx.navigateBack({
        delta: 1
      });
    });
  },
  getShopItem: function(element) {
    var that = this;
    return element.id == that.data.shop_id;
  }
})